package com.huawei.hwphy2d.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import com.huawei.hwphy2d.HwPhysicalSystem;
import com.huawei.hwphy2d.physical.util.DeviceParameters;

public class HwPhysicalSurfaceView extends SurfaceView implements Callback {
    private Canvas canvas;
    private DrawCallback drawCallback;
    private Thread drawerThread;
    private SurfaceHolder holder;
    private HwPhysicalSystem physicalSystem;

    public interface DrawCallback {
        void onDraw(Canvas canvas);
    }

    class DrawThread extends Thread {
        private SurfaceHolder holder;

        public DrawThread(SurfaceHolder surfaceHolder) {
            this.holder = surfaceHolder;
        }

        public void run() {
            while (HwPhysicalSurfaceView.this.drawCallback != null) {
                HwPhysicalSurfaceView.this.canvas = this.holder.lockCanvas(new Rect(0, 0, DeviceParameters.getScreenWidth(HwPhysicalSurfaceView.this.getContext()), DeviceParameters.getScreenHeight(HwPhysicalSurfaceView.this.getContext())));
                if (HwPhysicalSurfaceView.this.canvas != null) {
                    HwPhysicalSurfaceView.this.canvas.drawColor(0, Mode.CLEAR);
                    if (!(HwPhysicalSurfaceView.this.drawCallback == null || HwPhysicalSurfaceView.this.canvas == null)) {
                        HwPhysicalSurfaceView.this.drawCallback.onDraw(HwPhysicalSurfaceView.this.canvas);
                    }
                    this.holder.unlockCanvasAndPost(HwPhysicalSurfaceView.this.canvas);
                }
            }
        }
    }

    public interface ViewLayoutCallback {
        void onViewLayout(View view);
    }

    public HwPhysicalSurfaceView(Context context) {
        super(context);
        initView();
    }

    public HwPhysicalSurfaceView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        initView();
    }

    private void initView() {
        this.holder = getHolder();
        this.drawerThread = new Thread(new DrawThread(this.holder));
        this.holder.addCallback(this);
    }

    public void init(HwPhysicalSystem hwPhysicalSystem) {
        this.physicalSystem = hwPhysicalSystem;
        setZOrderOnTop(true);
        getHolder().setFormat(-3);
    }

    public void setWorldGravit(float f, float f2) {
        this.physicalSystem.setGravity(f, f2);
    }

    public void setDrawCallback(DrawCallback drawCallback) {
        this.drawCallback = drawCallback;
    }

    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        this.drawerThread.start();
    }

    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    }

    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        this.drawCallback = null;
    }
}
